package com.servlet;

import com.bean.Page;
import com.bean.Viedo;

import com.service.ViedoService;
import com.service.impl.ViedoServiceImpl;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "ClientViedoServlet",urlPatterns = "/clientViedoServlet.do")
public class ClientViedoServlet extends BaseServlet {
    private ViedoService viedoService = new ViedoServiceImpl();
    /**
     * 处理分页功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1 获取请求的参数 pageNo 和 pageSize
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        //2 调用GoodsService.page(pageNo，pageSize)：Page对象
        Page<Viedo> page = viedoService.page(pageNo,pageSize);
        page.setUrl("clientViedoServlet.do?action=page");
        //3 保存Page对象到Request域中
        request.setAttribute("page",page);
        //4 请求转发到pages/manager/book_manager.jsp页面
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
//    /**
//     * 处理分页功能
//     * @param req
//     * @param resp
//     * @throws ServletException
//     * @throws IOException
//     */
//    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //1 获取请求的参数 pageNo 和 pageSize
//        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
//        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
//        int min = WebUtils.parseInt(req.getParameter("min"), 0);
//        int max = WebUtils.parseInt(req.getParameter("max"), Integer.MAX_VALUE);
//
//        //2 调用GoodsService.page(pageNo，pageSize)：Page对象
////        Page<Viedo> page = viedoService.pageByPrice(pageNo,pageSize,min,max);
//
//        StringBuilder sb = new StringBuilder("clientGoodsServlet.do?action=pageByPrice");
//        // 如果有最小价格的参数,追加到分页条的地址参数中
//        if (req.getParameter("min") != null) {
//            sb.append("&min=").append(req.getParameter("min"));
//        }
//        // 如果有最大价格的参数,追加到分页条的地址参数中
//        if (req.getParameter("max") != null) {
//            sb.append("&max=").append(req.getParameter("max"));
//        }
//        page.setUrl(sb.toString());
//        System.out.println(page);
//        //3 保存Page对象到Request域中
//        req.setAttribute("page",page);
//        //4 请求转发到pages/manager/book_manager.jsp页面
//        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
//    }
}
